package com.xiaoshuaila.workbench.controller;

import com.xiaoshuaila.settings.domain.user;
import com.xiaoshuaila.settings.service.userservice;
import com.xiaoshuaila.settings.service.userserviceImpl;
import com.xiaoshuaila.tool.DateTimeUtil;
import com.xiaoshuaila.tool.PrintJson;
import com.xiaoshuaila.tool.ServiceFactory;
import com.xiaoshuaila.tool.SqlSessionUtil;
import com.xiaoshuaila.workbench.dao.Activitydao;
import com.xiaoshuaila.workbench.dao.TranDao;
import com.xiaoshuaila.workbench.dao.TranHistoryDao;
import com.xiaoshuaila.workbench.domain.Tran;
import com.xiaoshuaila.workbench.domain.TranHistory;
import com.xiaoshuaila.workbench.service.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Trancontroller extends HttpServlet {

    @Override
    public void service(HttpServletRequest req,
                        HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入交易控制器");
        String path=req.getServletPath();
        if ("/workbench/transaction/save.do".equals(path))
        {
            Activityservice activityservice=
                    (Activityservice) ServiceFactory.getService(new ActivityserviceImpl());
            System.out.println("save");
            req.setAttribute("ulist",activityservice.findalluser());
            req.getRequestDispatcher("/workbench/transaction/save" +
                    ".jsp").forward(req,resp);
        }
        else if("/workbench/transaction/getcustomername.do".equals(path))
        {
            Customerservice customerservice=
                    (Customerservice) ServiceFactory.getService(new CustomerserviceImpl());
            PrintJson.printJsonObj(resp,
                    customerservice.getcustomername(req.getParameter("name")));
        }
        else if("/workbench/transaction/save1.do".equals(path))
        {
            System.out.println("save1");
           String id=req.getParameter("id");
           String owner=req.getParameter("owner");
           String money=req.getParameter("money");
           String name=req.getParameter("name");
           String expectedDate=req.getParameter("expectedDate");
           String customerName=req.getParameter("customername");
           String stage=req.getParameter("stage");
           String type=req.getParameter("type");
           String source=req.getParameter("source");
           String activityId=req.getParameter("activityId");
           String contactsId=req.getParameter("contactsId");
           String createBy=((user)req.getSession().getAttribute(
                   "user")).getName();
           String createTime= DateTimeUtil.getSysTime();
           String description=req.getParameter("description");
           String contactSummary=req.getParameter("contactSummary");
           String nextContactTime=req.getParameter("nextContactTime");
            Tran tran=new Tran();
            tran.setId(id);
            tran.setOwner(owner);
            tran.setMoney(money);
            tran.setName(name);
            tran.setExpectedDate(expectedDate);
            tran.setStage(stage);
            tran.setType(type);
            tran.setSource(source);
            tran.setActivityId(activityId);
            tran.setContactsId(contactsId);
            tran.setCreateBy(createBy);
            tran.setCreateTime(createTime);
            tran.setDescription(description);
            tran.setContactSummary(contactSummary);
            tran.setNextContactTime(nextContactTime);
            Transervice transervice=
                    (Transervice) ServiceFactory.getService(new TranserviceImpl());
            boolean flag=transervice.save(tran,customerName);
            if (flag){
                resp.sendRedirect(req.getContextPath()+
                        "/workbench/transaction/index.jsp");
            }
        }
        else if("/workbench/transaction/detail.do".equals(path))
        {
            System.out.println("trandatail");
            System.out.println(req.getParameter("id"));
            Transervice transervice=
                    (Transervice) ServiceFactory.getService(new TranserviceImpl());
            Tran tran=transervice.detail(req.getParameter("id"));
            System.out.println(tran);
            Map<String,String>map=
                    (Map<String, String>) req.getServletContext().getAttribute("pmap");
            tran.setPossible(map.get(tran.getStage()));
            req.setAttribute("tran",tran);
            req.getRequestDispatcher("/workbench/transaction/detail" +
                    ".jsp").forward(req,resp);
        }
        else if("/workbench/transaction/gethistorylist.do".equals(path))
        {
            System.out.println("gethistorylist");
            System.out.println(req.getParameter("id"));
            Transervice transervice=
                    (Transervice) ServiceFactory.getService(new TranserviceImpl());
            Map<String,String>map=
                    (Map<String, String>) req.getServletContext().getAttribute("pmap");
            List<TranHistory>list=
                    transervice.findhistorylistbytranid(req.getParameter("id"));
            for (TranHistory t:list
                 ) {
                t.setPossible(map.get(t.getStage()));
            }
            System.out.println(req.getParameter("id"));
            System.out.println(list);
            PrintJson.printJsonObj(resp,list);
        }
        else if("/workbench/transaction/changestage.do".equals(path))
        {
            System.out.println("changestage");
            String id=req.getParameter("id");
            String money=req.getParameter("money");
            String stage=req.getParameter("stage");
            String expectedDate=req.getParameter("expectedDate");
            String editBy=((user)req.getSession().getAttribute(
                    "user")).getName();
            String editTime=DateTimeUtil.getSysTime();
            Tran tran=new Tran();

            tran.setId(id);
            tran.setMoney(money);
            tran.setStage(stage);
            tran.setExpectedDate(expectedDate);
            tran.setEditBy(editBy);
            tran.setEditTime(editTime);
            tran.setPossible(((Map<String,
                    String>)(req.getServletContext().getAttribute(
                    "pmap"))).get(stage));
            Transervice transervice=
                    (Transervice) ServiceFactory.getService(new TranserviceImpl());
            Boolean flag=transervice.changestage(tran);
            Map<String,Object>map=new HashMap<>();
            map.put("success",flag);
            map.put("tran",tran);
            System.out.println(id+","+money+","+stage+","+expectedDate+","+editBy+","+editTime);
            PrintJson.printJsonObj(resp,map);
        }
        else if("/workbench/transaction/getcharts.do".equals(path))
        {
            Transervice transervice=
                    (Transervice) ServiceFactory.getService(new TranserviceImpl());
            System.out.println("getcharts");
            List<Map>list=transervice.getchaet();
            System.out.println(list);
            List list1=new ArrayList();
            System.out.println(list1);
            for (Map map:list)
            {
                list1.add(map.get("name"));
            }
            Map map=new HashMap();
            map.put("la2",list1);
            map.put("la1",list);
            System.out.println(map);
            PrintJson.printJsonObj(resp,map);
        }
    }
}
